<?php
namespace App\Console\Commands\Task;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use App\Services\InnerApi;

use App\Repositories\Task\TaskSubmitRepository;

/**
 * 检测申诉记录是否已经超时
 * Class TaskOver
 * @package App\Console\Commands\Task
 * @version v1.3
 */
class TaskRejectTimeout extends Command
{
	protected $signature = 'task:rejectTimeout';

	protected $description = 'check rejection is timeout';

	private $logger;

	private $taskSubmitRepository;

	public function __construct(TaskSubmitRepository $taskSubmitRepository)
	{
		parent::__construct();
		$this->logger = Log::channel('console');
		$this->taskSubmitRepository = $taskSubmitRepository;
	}

	public function handle() {
		foreach ($this->taskSubmitRepository->getTimeoutReject() as $id) {
			try {
				$res = InnerApi::request('inner_reject_timeout', ['id' => $id]);
				if ( $res && $res['data'] == $id ) {
					$this->info($this->signature . "Set rejection:{$id} timeout.");
				} else {
					$this->info($this->signature . 'Call inner_reject_timeout api failed!');
				}
			} catch (\Exception $e) {
				$this->logger->error($this->signature, [$e->getMessage(), $id]);
				$this->error($e->getMessage());
			}
		}

		$this->info($this->signature . ' success！'. (new Carbon())->toDateTimeString());
	}
}